var isVerified = false;
var isLoadselectHospital = false; //load hospital

/**
 * 职称管理页面
 */
$(function() {
        $('#borrowInfoTable').bootstrapTable({
            url: '/borrow_info/getBorrowInfo.do',
            queryParams: {
                status: '1',
            },
            pagination: true,
            paginationLoop: false,
            pageNumber: 1,
            pageSize: 10,
            pageList: [10, 15, 20, 50, "all"],
            search: true,
            toolbar: "#userToolbar",
            clickToSelect: true,
            columns: [{
                checkbox: true
            }, {
                field: 'bookName',
                title: '书籍名称',
                halign: 'center',
            }, {
                field: 'stuName',
                title: '学生名称',
                halign: 'center',
            },  {
                field: 'beginTime',
                title: '借阅时间',
                halign: 'center',
            },  {
                field: 'bTime',
                title: '借阅时长（天）',
                halign: 'center',
            },  {
                field: 'endTime',
                title: '归还时间',
                halign: 'center',
            }, {
                field: 'orderStu',
                title: '预约人',
                halign: 'center',
            },  {
                field: 'createTime',
                title: '创建时间',
                halign: 'center',
            }],
            contextMenu: '#userContextMenu',
            onContextMenuItem: function(row, $el) {
                if ($el.data("item") == "delete") {
                    var data = {
                        ids: row.id,
                        status: 0
                    }
                    removeBorrowInfo(data);
                }
            }
        });


        /* 修改 */
        $("#backBook").click(function() {
            var objs = $('#borrowInfoTable').bootstrapTable('getSelections');
            if (objs.length == 0) {
                alertify.warning("请至少选择一项进行强制归还！");
                return;
            } else {
            	for(var i=0;i<objs.length;i++){
            		if(objs[i].endTime){
                		alertify.warning("不能强制归还已经归还的书籍！");
                		return;
                	}
            	}
                if (objs.length > 0) {
                    var ids = "";
                    for (var i = 0; i < objs.length; i++) {
                        ids += ',' + objs[i].id;
                    }
                    if (ids != '') {
                        ids = ids.substring(1);
                        var data = {
                            "ids": ids,
                        };
                        backBook(data);
                    }
                }
            }
        });

        /* 更改用户状态 */
        $("#removeBorrowInfo").click(function() {
            var objs = $('#borrowInfoTable').bootstrapTable('getSelections');
            if (objs.length == 0) {
                alertify.warning("请至少选择一项进行删除！");
                return;
            } else {
                if (objs.length > 0) {
                    var ids = "";
                    for (var i = 0; i < objs.length; i++) {
                        ids += ',' + objs[i].id;
                    }
                    if (ids != '') {
                        ids = ids.substring(1);
                        var data = {
                            "ids": ids,
                            "status": 0
                        };
                        removeBorrowInfo(data);
                    }
                }
            }
        });

        /* 关闭modal框 */
        $("#closeModal").click(function() {
            $("#bookModal").modal("hide");
        });

        /* 模态框提交 */
        $("#submit").click(function() {
            if ($("#name").val() == null || $("#name").val() == "") {
                $("#bookFormName").addClass("has-error");
                $("#errorName").html("名称不能为空！");
                return;
            }
            $("#bookFormName").removeClass("has-error");
            $("#errorName").html("");
            var data = getFormValue("bookForm");
            if (data.id == "") {
                url = "/book/save.do";
            } else {
                url = "/book/modify.do";
            }
            data.status = '1';
            $.ajax({
                type: "POST",
                url: url,
                // 参数，用户实体数据
                data: data,
                dataType: "json",
                success: function(res) {
                    if (res.code == "100") {
                        alertify.success("存储成功！");
                        $("#bookModal").modal("hide");
                        reload();
                    } else if (res.code == "201") {
                        alertify.warning("该书籍已经存在！");
                    } else if (res.code == "101") {
                        alertify.success("修改成功！！");
                        $("#bookModal").modal("hide");
                        reload();
                    }
                },
                error: function() {
                    alertify.error("网络传输错误，请检查网络再试");
                }
            });
        });
    })
    /**
     * 更新
     */
function modifyBook(data) {
    $.ajax({
        type: "POST",
        url: '/book/modify.do',
        data: data,
        dataType: "json",
        success: function(res) {
            if (res.code == "101") {
                alertify.success(res.msg);
                $("#bookModal").modal("hide");
                reload();
            } else {
                alertify.error(res.msg);
            }
        },
        error: function() {
            alertify.error("网络传输错误，请检查网络再试");
        }
    });
}

/**
 * 强制归还
 */
function backBook(data) {
    alertify.confirm('提示信息', "确定要强制归还吗？", function() {
        $.ajax({
            type: "POST",
            url: '/borrow_info/backBook.do',
            data: data,
            dataType: "json",
            success: function(res) {
                if (res.code == "102") {
                    alertify.success("归还成功");
                    reload();
                } else {
                    alertify.error("归还失败");
                }
            },
            error: function() {
                alertify.error("网络传输错误，请检查网络再试");
            }
        });
    }, function() {});
}
/**
 * 删除
 */
function removeBorrowInfo(data) {
    alertify.confirm('提示信息', "确定要删除吗？", function() {
        $.ajax({
            type: "POST",
            url: '/borrow_info/removeBorrowInfo.do',
            data: data,
            dataType: "json",
            success: function(res) {
                if (res.code == "102") {
                    alertify.success("删除成功");
                    reload();
                } else {
                    alertify.error("删除失败");
                }
            },
            error: function() {
                alertify.error("网络传输错误，请检查网络再试");
            }
        });
    }, function() {});
}
/**
 * 刷新数据
 */
function reload() {
    $('#borrowInfoTable').bootstrapTable('refresh', {
        url: '/borrow_info/getBorrowInfo.do'
    });
}

/**
 * 验证
 */
function doVerified() {
    $("#errorName").html("");
    if ($("#name").val() == null || $("#name").val() == "") {
        $("#errorName").html("名称不能为空！");
        return;
    }
    isVerified = true;
}